from time import sleep
from basic.device.Android import Android
from airtest.aircv import *
from airtest.core.api import *
from PIL import Image

# 要安装Tesseract-OCR.exe文字识别软件
import pytesseract


class couponPage(Android):

    @classmethod
    def clickCoupon(self):
        self.poco(text="我的优惠券").click()
        sleep(3)

    # 列表有无优惠券
    @classmethod
    def ifEmpty(self):
        no_bills = self.poco(text="暂无可用票券").wait(3)
        if no_bills.exists():
            return True
        else:
            return False


    @classmethod
    def getCoupon(self):
        sleep(3)
        coupon = self.poco(textMatches="有效期.*?").attr('text')
        print(coupon)
        return coupon

    @classmethod
    def getScreen(self):
        sleep(3)

        # 局部截图[0,315][1080,1501]
        screen = G.DEVICE.snapshot()
        local = aircv.crop_image(screen, (0, 315, 1080, 1051))

        # 保存截图
        pil_image = cv2_2_pil(local)
        pil_image.save("D:/dd/screen.png", quality=99, optimize=True)
        sleep(2)

        # 读取截图并识别文字，识别中文加参数lang='chi_sim'
        image = Image.open('D:/dd/screen.png')
        couponText = pytesseract.image_to_string(image, lang='chi_sim')

        print("-----------未使用优惠券界面--------------")
        print(couponText)
        return couponText
